package inftel.android;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class CoustomAdapter extends SimpleAdapter{

	private List<Usuario> listaAmigos;
	private boolean cargar;
	private int i;
	private String ip = "192.168.183.88";
	
	public CoustomAdapter(List<Usuario> lista,Context context, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		this.listaAmigos = lista;
		this.i = 0;
		this.cargar = true;
		// TODO Auto-generated constructor stub
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = super.getView(position, convertView, parent);
		if(cargar){
			Log.i("ENTRANDO",String.valueOf(position));
			View nombreView = v.findViewById(R.id.fila_amigos_listitem_title);
			String nombre = ((TextView) nombreView).getText().toString();
			
			View apellidosView = v.findViewById(R.id.fila_amigos_listitem_content);
			String apellidos = ((TextView) apellidosView).getText().toString();
			
			Iterator<Usuario> iter = getListaAmigos().iterator();
			while(iter.hasNext()){
				Usuario u = iter.next();
				if(u.getNombre().equals(nombre)&&u.getApellidos().equals(apellidos)){
					String enlaceFoto = u.getFoto();
					enlaceFoto = enlaceFoto.replace("localhost", ip);
					View imagen = v.findViewById(R.id.fila_amigos_listitem_pic);
					Log.i("ENLACE",enlaceFoto);
					Bitmap bm = Bitmap.createScaledBitmap(getBitmapFromURL(enlaceFoto),48,48,true);
					((ImageView)imagen).setImageBitmap(bm);
				}
			}
			i++;
			if(i>=(getListaAmigos().size())*11){cargar = false;i=0;}
			
		}
		
		return v;
	}

	public List<Usuario> getListaAmigos() {
		return listaAmigos;
	}

	public void setListaAmigos(List<Usuario> listaAmigos) {
		this.listaAmigos = listaAmigos;
	}
	
	public boolean isCargar() {
		return cargar;
	}

	public void setCargar(boolean cargar) {
		this.cargar = cargar;
	}

	public Bitmap getBitmapFromURL(String src) {
		
		Bitmap bm = null; 
		
	    try {
	        URL url = new URL(src);
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);
	        bm = myBitmap;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	    return bm;
	}

	
	/*public Bitmap loadBitmap(String url) {
	    Bitmap bitmap = null;
	    InputStream in = null;
	    BufferedOutputStream out = null;

	    try {
	        in = new BufferedInputStream(new URL(url).openStream(), 1024);

	        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
	        out = new BufferedOutputStream(dataStream, 1024);
	        CopyStream(in, out);
	        out.flush();

	        final byte[] data = dataStream.toByteArray();
	        BitmapFactory.Options options = new BitmapFactory.Options();
	        //options.inSampleSize = 1;

	        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
	    } catch (IOException e) {
	        Log.e("ERROR", "Could not load Bitmap from: " + url);
	    } finally {
	    	try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }

	    return bitmap;
	}
	
	public void CopyStream(InputStream is, OutputStream os)
    {
        final int buffer_size=1024;
        try
        {
            byte[] bytes=new byte[buffer_size];
            for(;;)
            {
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
            }
        }
        catch(Exception ex){}
    }	*/
}

